package com.usc.web;

import com.usc.po.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@RestController
//@RequestMapping("/user")
public class UserController {
    @RequestMapping("user")
    // WebFlux环境下，ServerWebExchange可直接注入
    public Mono<String> user(ServerWebExchange exchange) {
        int port = exchange.getRequest().getLocalAddress().getPort();
        return Mono.just("hello user " + port);
    }

    @Autowired
    WebClient.Builder builder;

    RestTemplate restTemplate = new RestTemplate();
    @RequestMapping("fromindex")
    public Mono<String> index(){
        return builder.build().get()
                .uri("http://index/index")
                .retrieve()
                .bodyToMono(String.class);
    }

    @RequestMapping("add")
    public Mono<Integer> add(@RequestParam("a") int a, @RequestParam("b") int b){
        return Mono.just(a+b);
    }

    @RequestMapping("hello/{name}")
    public Mono<String> hello(@PathVariable("name") String name){
        return Mono.just("hello " + name);
    }

    //使用请求参数传参
    @RequestMapping("emp1")
    public Emp emp1(Emp emp){
        emp.setAge(emp.getAge() + 18);
        return emp;
    }

    //使用body传参
    @RequestMapping("emp2")
    public Emp emp2(@RequestBody Emp emp){
        emp.setAge(emp.getAge() + 10);
        return emp;
    }
}
